<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use App\Http\Controllers\Controller;

class UserController extends Controller
{

    //用户列表+//用户查询
    public function userlist(Request $request)
    {
        $id=session('user_id');
        $search=$request->finduser;
        if ($search) {//有查询信息时
            $userlist=\DB::table('user')->where(array('user_name'=>$search))->orwhere(array('phone'=>$search))->paginate(15);
            foreach ($userlist as &$val) {
                $parents=array_reverse(explode(',', $val->pid));
                if (empty($parents[0])) {
                    $val->parentname="----";
                }else{
                    $val->parentname=\DB::table('user')->where(array('user_id'=>$parents[0]))->value('user_name');
                }
                if ($val->type==1) {
                    $val->role="省级代理";
                }elseif ($val->type==2) {
                    $val->role="市级代理";
                }else{
                    $val->role="普通会员";
                }
            }
            $usercount=\DB::table('user')->where(array('user_name'=>$search))->orwhere(array('phone'=>$search))->count();
        }else{//没有时全查询
            $userlist=\DB::table('user')->orderBy('user_id','asc')->paginate(15);
            foreach ($userlist as &$val) {
                $parents=array_reverse(explode(',', $val->pid));
                if (empty($parents[0])) {
                    $val->parentname="----";
                }else{
                    $val->parentname=\DB::table('user')->where(array('user_id'=>$parents[0]))->value('user_name');
                }
                if ($val->type==1) {
                    $val->role="省级代理";
                }elseif ($val->type==2) {
                    $val->role="市级代理";
                }else{
                    $val->role="普通会员";
                }
            }
            $usercount=\DB::table('user')->count();
        }
        return view('admin.user.user_list',compact('userlist','usercount'));
    }
    //封号
    public function userstop(Request $request)
    {
        $userid=session('user_id');
        $data=$request->all();
        $playerid=$data['playerid'];
        $res=\DB::table('user')->where(array('user_id'=>$playerid))->update(['status'=>'2']);
        if ($res) {
            $array = array('msg'=>'封号成功!','status'=>'true');
            return json_encode($array);
        }else{
            $array = array('msg'=>'封号失败!','status'=>'false');
            return json_encode($array);
        }
    }
    //解封
    public function userstart(Request $request)
    {
        $userid=session('user_id');
        $data=$request->all();
        $playerid=$data['playerid'];
        $res=\DB::table('user')->where(array('user_id'=>$playerid))->update(['status'=>'1']);
        if ($res) {
            $array = array('msg'=>'解封成功!','status'=>'true');
            return json_encode($array);
        }else{
            $array = array('msg'=>'解封失败!','status'=>'false');
            return json_encode($array);
        }
    }
    //设置代理页面
    public function changeproxy($userid){
        $userinfo=\DB::table('user')->where(array('user_id'=>$userid))->first();
        if ($userinfo->type==1) {
            $userinfo->level="省级代理";
        }elseif ($userinfo->type==2) {
            $userinfo->level="市级代理";
        }else{
            $userinfo->level="普通会员";
        }
        return view('admin.user.user_update',compact('userinfo'));
    }
    //设置代理ajax调用
    public function changeproxyed(Request $request){
        $putinfo=$request->all();
        if ($putinfo['proxy']=='1') {//设置为省级代理
            $res=\DB::table('user')->where(array('user_id'=>$putinfo['userid']))->update(['type'=>$putinfo['proxy']]);
            //断开与上级的链接
            $res2=\DB::table('user')->where(array('user_id'=>$putinfo['userid']))->update(['pid'=>'']);
            if ($res) {
                $array = array('msg'=>'设置成功!','status'=>'true');
                return json_encode($array);
            }else{
                $array = array('msg'=>'设置失败!','status'=>'false');
                return json_encode($array);
            }
        }else{//将为普通会员
            $res=\DB::table('user')->where(array('user_id'=>$putinfo['userid']))->update(['type'=>$putinfo['proxy']]);
            if ($res) {
                $array = array('msg'=>'设置成功!','status'=>'true');
                return json_encode($array);
            }else{
                $array = array('msg'=>'设置失败!','status'=>'false');
                return json_encode($array);
            }
        }
    }
    //代理列表+//代理查询
    public function proxylist(Request $request){
        $id=session('user_id');
        $search=$request->findproxy;
        if ($search) {//有查询信息时
            $proxylist=\DB::table('user')->where(array('user_name'=>$search))->orwhere(array('phone'=>$search))->whereIn('type',[1,2])->paginate(15);
            foreach ($proxylist as &$val) {
                $parents=array_reverse(explode(',', $val->pid));
                if (empty($parents[0])) {
                    $val->parentname="----";
                }else{
                    $val->parentname=\DB::table('user')->where(array('user_id'=>$parents[0]))->value('user_name');
                }
                if ($val->type==1) {
                    $val->role="省级代理";
                }elseif ($val->type==2) {
                    $val->role="市级代理";
                }else{
                    $val->role="普通会员";
                }
                //获取推荐人数
                $val->recommend=\DB::table('user')->where('pid','like','%'.','.$val->user_id)->count();
            }
            $proxycount=\DB::table('user')->where(array('user_name'=>$search))->orwhere(array('phone'=>$search))->whereIn('type',[1,2])->count();
        }else{//没有时全查询
            $proxylist=\DB::table('user')->whereIn('type',[1,2])->orderBy('user_id','asc')->paginate(15);
            foreach ($proxylist as &$val) {
                $parents=array_reverse(explode(',', $val->pid));
                if (empty($parents[0])) {
                    $val->parentname="----";
                }else{
                    $val->parentname=\DB::table('user')->where(array('user_id'=>$parents[0]))->value('user_name');
                }
                if ($val->type==1) {
                    $val->role="省级代理";
                }elseif ($val->type==2) {
                    $val->role="市级代理";
                }else{
                    $val->role="普通会员";
                }
                //获取推荐人数
                $val->recommend=\DB::table('user')->where('pid','like','%'.','.$val->user_id)->count();
            }
            $proxycount=\DB::table('user')->whereIn('type',[1,2])->count();
        }
        return view('admin.user.proxy_list',compact('proxylist','proxycount'));
    }

    //管理员列表
    public function adminlist(Request $request){
        $search=$request->findadmin;
        if ($search) {//有查询信息时
            $adminlist=\DB::table('admin')->where(array('name'=>$search))->paginate(15);
            foreach ($adminlist as &$val) {
                if ($val->type==1) {
                    $val->role="超级管理员";
                }else{
                    $val->role="普通管理员";
                }
            }
            $admincount=\DB::table('admin')->where(array('name'=>$search))->count();
        }else{//没有时全查询
            $adminlist=\DB::table('admin')->orderBy('type','asc')->paginate(15);
            foreach ($adminlist as &$val) {
                if ($val->type==1) {
                    $val->role="超级管理员";
                }else{
                    $val->role="普通管理员";
                }
            }
            $admincount=\DB::table('admin')->count();
        }
        return view('admin.user.admin_list',compact('adminlist','admincount'));
    }
    //密码重置
    public function passwordreset(Request $request){
        $userid=session('user_id');
        $adminid=$request->adminid;
        $newpassword=Hash::make('123456');
        $res=\DB::table('admin')->where(array('id'=>$adminid))->update(['password'=>$newpassword]);
        if ($userid==$adminid) {
            if ($res) {
                $array = array('msg'=>'设置成功!','status'=>'true','codes'=>'1');
                return json_encode($array);
            }else{
                $array = array('msg'=>'设置失败!','status'=>'false');
                return json_encode($array);
            }
        }else{
            if ($res) {
                $array = array('msg'=>'设置成功!','status'=>'true','codes'=>'2');
                return json_encode($array);
            }else{
                $array = array('msg'=>'设置失败!','status'=>'false');
                return json_encode($array);
            }
        }
    }
    //删除管理员
    public function admindelete(Request $request){
        $userid=session('user_id');
        $adminid=$request->adminid;
        $res=\DB::table('admin')->where(array('id'=>$adminid))->delete();
        if ($userid==$adminid) {
            if ($res) {
                $array = array('msg'=>'删除成功!','status'=>'true','codes'=>'1');
                return json_encode($array);
            }else{
                $array = array('msg'=>'删除失败!','status'=>'false');
                return json_encode($array);
            }
        }else{
            if ($res) {
                $array = array('msg'=>'删除成功!','status'=>'true','codes'=>'2');
                return json_encode($array);
            }else{
                $array = array('msg'=>'删除失败!','status'=>'false');
                return json_encode($array);
            }
        }
    }
    //添加管理员
    public function adminadd(){
        if (!empty($_POST)) {
            //$admininfo=$request->all();
            $adminname=$_POST['adminname'];
            $adminpassword=$_POST['adminpassword'];
            $readminpassword=$_POST['readminpassword'];
            $selected=$_POST['selected'];
            if (empty($adminname) || empty($adminpassword) || empty($readminpassword)) {
                $array = array('msg'=>'账户名、密码和确认密码均不能为空!','status'=>'false');
                return json_encode($array);
            }elseif (\DB::table('admin')->where(array('name'=>$adminname))->first()) {
                $array = array('msg'=>'该账户名已存在,请更换账户名!','status'=>'false');
                return json_encode($array);
            }elseif ($adminpassword!=$readminpassword) {
                $array = array('msg'=>'两次密码输入不一致!','status'=>'false');
                return json_encode($array);
            }else{
                // $data['name']=$admininfo->adminname;
                // $data['password']=Hash::make($admininfo->adminpassword);
                // $data['type']=$admininfo->selected;
                // $data['addtime']=date('Y-m-d H:i:s',time());
                $res=\DB::table('admin')->insert([
                    ['name'=>$adminname,'password'=>Hash::make($adminpassword),'type'=>$selected,'addtime'=>date('Y-m-d H:i:s',time())],
                ]);
                if ($res) {
                    $array = array('msg'=>'添加成功!','status'=>'true');
                    return json_encode($array);
                }else{
                    $array = array('msg'=>'添加失败!','status'=>'false');
                    return json_encode($array);
                }
            }
        }else{
            return view('admin.user.admin_add');
        }
    }
}